# Compiler and flags
CC = gcc
CFLAGS = -Wall -Wextra -O0 -g2 -ggdb

# Variables
SRC_DIR = .
LIB_DIR = ../build/srv/lib
TOOLS_DIR = ../build/srv/tools
TARGET ?= wdt_jh  # 默认编译目标为 wdt_jh
SRC = $(SRC_DIR)/$(TARGET).c
OBJ = $(TARGET).o
STATIC_LIB = lib$(TARGET).a
SHARED_LIB = lib$(TARGET).so
ifeq ($(TARGET),wdt_tyc)
    BINARY = hardWdtTyc
else ifeq ($(TARGET),wdt_jh)
    BINARY = hardWdtJh
else
	BINARY = hard$(TARGET)
endif

.PHONY: all build liba libso bin install clean

all: build

# 构建所有目标
build: liba libso bin

# 编译静态库
liba:
	$(CC) -c $(SRC) -o $(OBJ)
	ar rcs $(STATIC_LIB) $(OBJ)
	rm $(OBJ)
	@echo "Built static library $(STATIC_LIB)"

# 编译共享库
libso:
	$(CC) -fPIC -shared -g -o $(SHARED_LIB) $(SRC)
	@echo "Built shared library $(SHARED_LIB)"

# 编译可执行文件
bin:
	$(CC) $(CFLAGS) -D__MAIN__ -I$(SRC_DIR) -g $(SRC) -o $(BINARY)
	@echo "Built binary $(BINARY)"

# 安装目标到指定目录
install:
	@mkdir -p $(LIB_DIR) $(TOOLS_DIR)
	cp -f $(SHARED_LIB) $(LIB_DIR)
	cp -f $(BINARY) $(TOOLS_DIR)
	@echo "Installed $(SHARED_LIB) and $(BINARY)"

# 清理目标文件
clean:
	rm -f *.a *.so *.o hardwdt_* libwdt_*
	@echo "Cleaned all targets"
